#include "botcode.h"

codeline::codeline() {

}

codeline::codeline(wxString cmd, int cmdint) {
  command = cmd;
  commandINT = cmdint;
  return;
}

botcode::botcode() {

}

botcode::~botcode() {
  for (unsigned int i = 0; i < code.size(); i++) {
    delete code[i];
  }
  return;
}

// returns the name of the bot
wxString botcode::readfile(wxString filename) {
  wxTextFile * file = new wxTextFile(filename);
  wxString botName = wxEmptyString;
  if (file->Exists()) {
    if (!file->IsOpened()) {
      file->Open();
      // get the name of the superbot (first line)
      wxString str = file->GetFirstLine();
      botName = str;
      // file is opened, now loop through all the lines of the file
      for (str = file->GetNextLine(); !file->Eof(); str = file->GetNextLine()) {
        while ((str[0] == '\n' || str[0] == ';') && !file->Eof()) str = file->GetNextLine(); // skip newlines
        if (file->Eof()) break;
        // we now have a line of code
        str.Lower(); // make all lower case
        str.Replace(_(" "), _(""), true); // delete all white space
        int commas = 0;
        for (unsigned int i = 0; i < str.Len(); i++) if (str[i] == ',') commas++; // count the commas, and thus the arguments (+1)
        wxString command = str.BeforeFirst(',');

        // Read the arguments from the string
        wxString arg1, arg2, arg3;
        if (commas > 0) {
          wxString args = str.AfterFirst(',');
          arg1 = args.BeforeFirst(',');
          if (commas > 1) {
            wxString lastargs = args.AfterFirst(',');
            arg2 = lastargs.BeforeFirst(',');
            if (commas > 2) arg3 = lastargs.AfterFirst(',');
          }
        }

        // now build a codeline from the line read from the file
        bool correct_command = true;
        codeline * newline;

        if (!command.compare(_("move"))) newline = new codeline(command, CMD_MOVE);
        else if (!command.compare(_("turn"))) newline = new codeline(command, CMD_TURN);
        else if (!command.compare(_("store"))) newline = new codeline(command, CMD_STORE);
        else if (!command.compare(_("add"))) newline = new codeline(command, CMD_ADD);
        else if (!command.compare(_("addm"))) newline = new codeline(command, CMD_ADDM);
        else if (!command.compare(_("ifeq"))) newline = new codeline(command, CMD_IFEQ);
        else if (!command.compare(_("ifneq"))) newline = new codeline(command, CMD_IFNEQ);
        else if (!command.compare(_("ifeqm"))) newline = new codeline(command, CMD_IFEQM);
        else if (!command.compare(_("ifneqm"))) newline = new codeline(command, CMD_IFNEQM);
        else if (!command.compare(_("jump"))) newline = new codeline(command, CMD_JUMP);
        else if (!command.compare(_("jumpm"))) newline = new codeline(command, CMD_JUMPM);
        else if (!command.compare(_("attack"))) newline = new codeline(command, CMD_ATTACK);
        else {
          wxMessageBox(wxString::Format(_("command \"%s\" unknown! Skipping line..."), command.c_str()), _("Error"), wxICON_ERROR);
          correct_command = false;
        }
        if (correct_command) {
          unsigned long arg;
          arg1.ToULong(&arg);
          newline->intPrm1 = ((int) arg);
          arg2.ToULong(&arg);
          newline->intPrm2 = ((int) arg);
          arg3.ToULong(&arg);
          newline->intPrm3 = ((int) arg);
          addLine(newline);
        }
      } // Loop to next line in file
    }
  } else {
    wxMessageBox(wxString::Format(_("The file \"%s\" could not be opened!"), filename.c_str()), _("Error"), wxICON_ERROR);
  }
  return botName;
}

void botcode::addLine(codeline * line) {
  code.push_back(line);
  return;
}

void botcode::printCode() {

}

wxString botcode::getCode() {
  // Bart: I made this very simple, could you make sure this works for all code? Ramon
  wxString tmpCode;
  for (unsigned int i = 0; i < code.size(); i++) {
    wxString command = code[i]->command;
    tmpCode.Append(wxString::Format(_("%s, %d, %d, %d\n"), command.c_str(), code[i]->intPrm1, code[i]->intPrm2, code[i]->intPrm3));
  }
  return tmpCode;
}
